package org.rpgmapper.view;

import org.rpgmapper.Main;
import org.rpgmapper.R;
import org.rpgmapper.bo.Bonus;
import org.rpgmapper.bo.Character;
import org.rpgmapper.skirmish.SkirmishManager;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.rpgmapper.bo.Stat;

public class MenuInfoView extends FrameLayout {

    private View view;
    private View viewLand;
    private Character character;
    private boolean isVisible = false;
    DisplayMetrics metrics = new DisplayMetrics();
    private int navigationbarHeight = 48;

    public MenuInfoView(Context context) {
        super(context);
        //setBackgroundColor(Color.RED);
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        this.setPivotX(0f);
        this.setPivotY(0);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.menu_info, null);
        viewLand = inflater.inflate(R.layout.menu_info_land, null);
    }

    public MenuInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //setBackgroundColor(Color.RED);
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        this.setPivotX(0f);
        this.setPivotY(0f);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.menu_info, null);
        viewLand = inflater.inflate(R.layout.menu_info_land, null);
    }

    public void setCharacter(Character c) {
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) this.getLayoutParams();
        lp.setMargins(0, 0, 0, 0);
        this.setTranslationX(0f);
        this.setTranslationY(0f);
        this.setRotation(0f);
        this.character = c;
        this.removeAllViews();
        int hauteur = metrics.heightPixels - navigationbarHeight;
        int largeur = 400;//(metrics.heightPixels)/2;
        switch (character.getSide()) {
            case 1://gauche
                this.setLayoutParams(new RelativeLayout.LayoutParams(hauteur - ((Main) getContext()).getSkirmishInfoVisibleHeight(), largeur));
                this.setRotation(90f);
                this.setTranslationX(largeur);
                this.setTranslationY(0f);
                this.addView(viewLand);
                break;
            case 2://haut
                // la rotation ne change pas le sens des margins
                this.setLayoutParams(new RelativeLayout.LayoutParams(largeur, hauteur - ((Main) getContext()).getSkirmishInfoVisibleHeight()));
                this.setRotation(180f);
                this.setTranslationX(largeur);
                this.setTranslationY(hauteur - ((Main) getContext()).getSkirmishInfoVisibleHeight());
                this.addView(view);
                break;
            case 3://droite
                this.setLayoutParams(new RelativeLayout.LayoutParams(hauteur - ((Main) getContext()).getSkirmishInfoVisibleHeight(), largeur));
                this.addView(viewLand);
                this.setRotation(-90f);
                this.setTranslationX(metrics.widthPixels - largeur);
                this.setTranslationY(hauteur - ((Main) getContext()).getSkirmishInfoVisibleHeight());
                break;
            case 4://bas
                this.setLayoutParams(new RelativeLayout.LayoutParams(largeur, hauteur - ((Main) getContext()).getSkirmishInfoVisibleHeight()));
                this.setRotation(0f);
                this.setTranslationX(0f);
                this.setTranslationY(0f);
                this.addView(view);
                //lp = (RelativeLayout.LayoutParams)this.getLayoutParams();
                //lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, ((Main)getContext()).getSkirmishInfoVisibleHeight());
                break;
            default:
                //this.addView(glView);
                break;
        }
        ((TextView) this.findViewById(R.id.character_name)).setText(c.getName());
        ((TextView) this.findViewById(R.id.pv_value)).setText(c.get(Stat.HitPoints) + "/" + c.get(Stat.MaxHitPoints));
        ((TextView) this.findViewById(R.id.init_value)).setText(c.get(Stat.Init) + "");
        ((TextView) this.findViewById(R.id.depl_value)).setText(c.get(Stat.MovePoints));
        ((TextView) this.findViewById(R.id.ca_value)).setText(c.get(Stat.AC) + "");
        String bonusStr = "";
        System.err.println("size = " + c.getBonus().size());
        if (c != null && c.getBonus() != null && c.getBonus().size() > 0) {
            for (Bonus b : c.getBonus()) {
                bonusStr += b.getEffect() + " (" + b.getDurationBeforeEnd() + ")" + " | ";
            }
            bonusStr = bonusStr.substring(0, bonusStr.length() - 3);
        }
        System.err.println(bonusStr);
        ((TextView) this.findViewById(R.id.bonus_label)).setText(bonusStr);
        ((ImageButton) this.findViewById(R.id.cancel)).setOnClickListener(cancelClickListener);
        ((ImageButton) this.findViewById(R.id.ok)).setOnClickListener(okClickListener);
    }

    public void toggle() {
        if (isVisible) {
            /*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f);
            alpha.setDuration(300);
            alpha.start();*/
            this.setVisibility(View.GONE);
            isVisible = false;
        } else {
            /*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f);
            alpha.setDuration(100);
            alpha.start();*/
            this.setVisibility(View.VISIBLE);
            isVisible = true;
        }
    }

    public void hide() {
        if (isVisible) {
            /*ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f);
            alpha.setDuration(100);
            alpha.start();*/
            this.setVisibility(View.GONE);
            isVisible = false;
        }
    }
    private OnClickListener cancelClickListener = new OnClickListener() {

        public void onClick(View v) {
            MenuInfoView.this.hide();
        }
    };
    private OnClickListener okClickListener = new OnClickListener() {

        public void onClick(View v) {
            SkirmishManager.getInstance().nextCharacter();
            MenuInfoView.this.hide();
        }
    };
}
